﻿//-----------------------------------------------------------------------------------------------------------
// Wbfs Engine (WBFSe) Project by Omega Frost 
// http://wbfse.codeplex.com/
//
// WBFSe is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections;

namespace WBFSe.Attributes
{
    /// <summary>
    /// Read-only usage table
    /// </summary>
    public class UsageTable
    {
        // Variables

        BitArray usage;
        int used;
        int last;

        // Properties

        /// <summary>
        /// Indexer
        /// </summary>
        /// <param name="index">Sector index</param>
        /// <returns>The usage of the selected sector</returns>
        public bool this[int index]
        {
            get { return this.usage[index]; }
        }

        /// <summary>
        /// The number of sectors
        /// </summary>
        public int Count
        {
            get { return this.usage.Count; }
        }

        /// <summary>
        /// The number of used sectors
        /// </summary>
        public int UsedSectors
        {
            get { return this.used; }
        }

        /// <summary>
        /// The last used sector index
        /// </summary>
        public int LastUsedSector
        {
            get { return this.last; }
        }

        // Methods

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="array">Usage array</param>
        public UsageTable(Boolean[] array)
        {
            this.usage = new BitArray(array);
            this.used = 0;
            this.last = -1;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i])
                {
                    this.used++;
                    this.last = i;
                }
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="table">Another usage table</param>
        public UsageTable(UsageTable table)
        {
            this.usage = table.usage;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="table">Another usage table</param>
        /// <param name="ratio">Grouping ratio</param>
        public UsageTable(UsageTable table, int ratio)
        {
            this.usage = new BitArray(table.Count / ratio);
            this.usage.SetAll(false);
            this.used = 0;
            this.last = -1;

            for (int i = 0; i < table.Count; i++)
            {
                if (table.usage[i])
                {
                    int j = i / ratio;
                    if (!this.usage[j])
                    {
                        this.usage[j] = true;
                        this.used++;
                        this.last = j;
                    }
                }
            }
        }
    }
}
